package com.cloudkinto.common.utils.ali;

import com.aliyun.oss.*;
import com.aliyun.oss.model.CopyObjectRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.cloudkinto.common.common.DateUtils;
import com.cloudkinto.common.constant.GlobalConstants;
import com.cloudkinto.common.utils.TimeUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.InputStream;

/**
 * @author fanp
 * @version v1.0.0
 * @Description :
 * @Create on : 2021/2/2 16:40
 **/
public class AliOss {
    //aliCloud 相关配置（point 为杭州）(内网传输节点)
    private static String endpoint = "oss-cn-hangzhou.aliyuncs.com";//外网
    //    private static String endpoint = "oss-cn-hangzhou-internal.aliyuncs.com";//内网
    private static String accessKeyId = "LTAI4GACEnVYG5jtk7AsZsH6";
    private static String accessKeySecret = "FdFWRYy5W3jkV7hhricpzinLl4XR8n";
    private static String bucketName = "cloudkinto-1";
    public final static String objectName = "cloudkint-file/";
    public final static String template = "cloudkint-file/template/";
    public final static String temporaryFile = "temporaryFile/";
    private static String objectNameTemporary = "";

    //上传文件到阿里Oss (从临时文件夹中复制到现文件夹中 并删除临时文件夹中的文件)
    public static void uploadFile(String path, String suffix, Long fileId) {
        if (GlobalConstants.IMG_SUFFIX.contains(suffix)) {
            objectNameTemporary = objectName + DateUtils.getTodayYYMMStr() + "/" + "photo" + "/" + fileId + "." + suffix;
        } else {
            objectNameTemporary = objectName + DateUtils.getTodayYYMMStr() + "/" + "document" + "/" + fileId + "." + suffix;
        }
        //创建OSSclient
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        //复制  在同一个域下的不同文件夹就行复制  eg:"cloudkint-file/temporaryFile/3118.png"
        CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucketName, path, bucketName, objectNameTemporary);
        //判断这个文件是否存在
        if (!oss.doesObjectExist(bucketName, path)) {
            return;
        }
        oss.copyObject(copyObjectRequest);
        //将临时文件删除  注意：阿里OSS删除  默认如果子文件夹下面无内容会将子文件夹一起删除
        oss.deleteObject(bucketName, path);
        // 关闭OSSClient
        oss.shutdown();
    }

    //文件图片直接上传到阿里云Oss
    public static void uploadFile(InputStream inputStream,String suffix, Long fileId){
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        if (GlobalConstants.IMG_SUFFIX.contains(suffix)) {
            objectNameTemporary = objectName + DateUtils.getTodayYYMMStr()  + "/" + "photo" + "/" + fileId + "." + suffix;
        } else {
            objectNameTemporary = objectName + DateUtils.getTodayYYMMStr()  + "/" + "document" + "/" + fileId + "." + suffix;
        }
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectNameTemporary, inputStream);
        try {
            oss.putObject(putObjectRequest);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (oss != null) {
                oss.shutdown();
            }
        }
    }

    //设置一个存储临时 都可存
    public static void temporaryFile(Long fileId, String suffix, MultipartFile file) throws Exception {
        //创建一个oss实例
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        objectNameTemporary = objectName + "temporaryFile/" + fileId + "." + suffix;
        //上传
        InputStream inputStream = file.getInputStream();
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectNameTemporary, inputStream);
        oss.putObject(putObjectRequest);


        // 上传后检查
        if (oss.doesObjectExist(bucketName, objectNameTemporary)) {
            System.out.println("File uploaded successfully to OSS: {}");
            System.out.println( objectNameTemporary);
        } else {
            System.out.println("File upload failed:");
            System.out.println(objectNameTemporary);
        }
        oss.shutdown();

        // 输出外网访问 URL
        String url = "https://" + bucketName + "." + endpoint + "/" + objectNameTemporary;
        System.out.println("File can be accessed at: " + url);
    }
//E:\\tools\\nginx\\nginx-1.23.3\\file0\\\69192.png
    public static void temporaryFile2(Long fileId, String suffix, File file) throws Exception {
        //创建一个oss实例
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        objectNameTemporary = objectName + "temporaryFile/" + fileId + "." + suffix;
        //上传
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectNameTemporary, file);
        oss.putObject(putObjectRequest);
        oss.shutdown();
    }

    /**
     * @param fileName
     * @param file
     * @return 文件名
     * @throws Exception
     */
    public static String temporaryFile2(String fileName, File file) throws Exception {
        //创建一个oss实例
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        objectNameTemporary = objectName + "temporaryFile/" + fileName;
        //上传
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectNameTemporary, file);
        oss.putObject(putObjectRequest);
        oss.shutdown();
        if (file.exists()) {
            file.delete();
        }
        return "temporaryFile/" + fileName;
    }

    public static void temporaryFile3(Long fileId, String suffix, InputStream inputStream) {
        ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
        // 设置OSSClient允许打开的最大HTTP连接数，默认为1024个。
        conf.setMaxConnections(1024);
        // 设置Socket层传输数据的超时时间，默认为50000毫秒。
        conf.setSocketTimeout(10000);
        // 设置建立连接的超时时间，默认为50000毫秒。
        conf.setConnectionTimeout(10000);
        // 设置从连接池中获取连接的超时时间（单位：毫秒），默认不超时。
        conf.setConnectionRequestTimeout(1000);
        // 设置连接空闲超时时间。超时则关闭连接，默认为60000毫秒。
        //        conf.setIdleConnectionTime(10000);
        // 设置失败请求重试次数，默认为3次。
        conf.setMaxErrorRetry(3);
        //创建一个oss实例
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, conf);
        objectNameTemporary = objectName + "temporaryFile/" + fileId + "." + suffix;
        //上传
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectNameTemporary, inputStream);
        try {
            oss.putObject(putObjectRequest);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (oss != null) {
                oss.shutdown();
            }
        }
    }


    //删除文件
    public static void deleteFile(String path, String suffix, Long fileId) {
        if (GlobalConstants.IMG_SUFFIX.contains(suffix)) {
            objectNameTemporary = objectName + DateUtils.getTodayYYMMStr() + "/" + "photo" + "/" + fileId + "(已删除)." + suffix;
        } else {
            objectNameTemporary = objectName + DateUtils.getTodayYYMMStr() + "/" + "document" + "/" + fileId + "(已删除)." + suffix;
        }
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucketName, path, bucketName, objectNameTemporary);
        //判断这个文件是否存在
        if (!oss.doesObjectExist(bucketName, path)) {
            return;
        }
        //暂未有修改文件名的方法 需要copy 然后再修改
        oss.copyObject(copyObjectRequest);
        oss.deleteObject(bucketName, path);
        oss.shutdown();
    }
}
